package com.gzhryc.shared_device.oem.h5.mng.controllers.cdcz;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdcz.code.CDCZCostConfig;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZFeeTemplate;
import com.gzhryc.shared_device.cdcz.code.dao.models.CDCZPowerIntervalMoneyContent;
import com.gzhryc.shared_device.cdcz.code.services.CDCZFeeTemplateService;
import com.gzhryc.shared_device.oem.code.ConfigUtils;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;

public class CDCZDeviceCostController {

    @Route(value = { "/cdcz/device/cost.html" })
    public String cost(HttpServletRequest request) {
        CDCZCostConfig config = ConfigUtils.getCDCZCostConfig();
        WebUtils.setAttribute(request,config);
        return "/cdcz/cost/config.html";
    }

    @Route(value = { "/cdcz/device/cost/save.do" })
    public AjaxResult doCostSave(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "保存失败");
        CDCZCostConfig config = WebUtils.getParameter(request, CDCZCostConfig.class);
        if(config != null){
            Double minWalletMoneyStr = WebUtils.getDouble(request,"minWalletMoneyStr");
            if(minWalletMoneyStr == null || minWalletMoneyStr < 0.01 || minWalletMoneyStr > 3){
                result.setMsg("钱包最小金额设置范围在0.01-3元");
                return result;
            }
            config.setMinWalletMoney(NumberTools.changeMoneyLi(minWalletMoneyStr));

            //不更新，使用旧值覆盖
            CDCZCostConfig oldConfig = ConfigUtils.getCDCZCostConfig();
            config.setPimContent(oldConfig.getPimContent());

            if(ConfigUtils.save(config)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        }
        return result;
    }

    @Route(value = "/cdcz/device/cost/power_interval/list.data")
    public ListResult<CDCZPowerIntervalMoneyContent.Item> powerInterval(HttpServletRequest request) {
        ListResult<CDCZPowerIntervalMoneyContent.Item> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        CDCZCostConfig config = ConfigUtils.getCDCZCostConfig();
        if(StringTools.isNotBlank(config.getPimContent())){
            CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(config.getPimContent(),CDCZPowerIntervalMoneyContent.class);
            if(content.getItems() != null && content.getItems().size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
                result.getBody().setData(content.getItems());
            }
        }
        return result;
    }

    @Route(value = { "/cdcz/device/cost/power_interval/add.html" })
    public String addSetMeal(HttpServletRequest request) {
        CDCZCostConfig config = ConfigUtils.getCDCZCostConfig();
        if(StringTools.isNotBlank(config.getPimContent())) {
            CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(config.getPimContent(), CDCZPowerIntervalMoneyContent.class);
            if (content.getItems() != null && content.getItems().size() > 0) {
                CDCZPowerIntervalMoneyContent.Item item = content.getItems().get(content.getItems().size() - 1);
                request.setAttribute("minPower",item.getMaxPower());
            }
        }
        return "/cdcz/cost/power_interval.html";
    }

    @Route(value = { "/cdcz/device/cost/power_interval/add.do" })
    public AjaxResult doAddSetMeal(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增功率区间收费失败");
        CDCZPowerIntervalMoneyContent.Item item = WebUtils.getParameter(request, CDCZPowerIntervalMoneyContent.Item.class);
        if(item != null && item.getMaxPower() != null && item.getMinPower() != null){
            Double moneyStr = WebUtils.getDouble(request,"moneyStr");
            if(moneyStr == null || moneyStr < 0.01 || moneyStr > 99){
                result.setMsg("功率区间金额设置范围在0.01-99元");
                return result;
            }
            if(item.getMaxPower() <= item.getMinPower()){
                result.setMsg("功率区间错误");
                return result;
            }
            item.setMoney(NumberTools.changeMoneyLi(moneyStr));
            try {
                if(ConfigUtils.addCDCZCostConfigPimContentItem(item)){
                    result.setCode(DtoConstants.SUCCESS,null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/cdcz/device/cost/power_interval/update.html" })
    public String updateSetMeal(HttpServletRequest request) {
        Double minPower = WebUtils.getDouble(request,"minPower");
        if(minPower != null) {
            CDCZCostConfig config = ConfigUtils.getCDCZCostConfig();
            if(StringTools.isNotBlank(config.getPimContent())) {
                CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(config.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                if (content.getItems() != null && content.getItems().size() > 0) {
                    for(CDCZPowerIntervalMoneyContent.Item item : content.getItems()){
                        if(item.getMinPower().equals(minPower)){
                            WebUtils.setAttribute(request,item);
                            break;
                        }
                    }
                }
            }
        }
        return "/cdcz/cost/power_interval.html";
    }

    @Route(value = { "/cdcz/device/cost/power_interval/update.do" })
    public AjaxResult doUpdateSetMeal(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改功率区间收费失败");
        CDCZPowerIntervalMoneyContent.Item item = WebUtils.getParameter(request, CDCZPowerIntervalMoneyContent.Item.class);
        if(item != null && item.getMaxPower() != null && item.getMinPower() != null) {
            Double moneySer = WebUtils.getDouble(request, "moneyStr");
            if(moneySer == null || moneySer < 0.01 || moneySer > 99){
                result.setMsg("功率区间金额设置范围在0.01-99元");
                return result;
            }
            if(item.getMaxPower() <= item.getMinPower()){
                result.setMsg("功率区间错误");
                return result;
            }
            item.setMoney(NumberTools.changeMoneyLi(moneySer));
            try {
                if(ConfigUtils.updateCDCZCostConfigPimContentItem(item)){
                    result.setCode(DtoConstants.SUCCESS,null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/cdcz/device/cost/power_interval/del.do" })
    public AjaxResult doDelSetMeal(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除功率区间收费失败");
        Double minPower = WebUtils.getDouble(request,"minPower");
        if(minPower != null) {
            try {
                if (ConfigUtils.deleteCDCZCostConfigPimContentItem(minPower)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    //网点配置价格

    @Route(value = { "/cdcz/place/cost.html" })
    public String placeCost(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request,"placeId");
        CDCZFeeTemplate feeTemplate = null;
        CDCZPowerIntervalMoneyContent content = null;
        PlaceBusinessConfig deviceFunc = PlaceBusinessConfigService.self().getByKeys(placeId, EBusinessType.CDCZ.index());
        if(deviceFunc != null && deviceFunc.getFeeTemplateId() != null){
            feeTemplate = CDCZFeeTemplateService.self().getJoinById(deviceFunc.getFeeTemplateId());
        }

        PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
        if(place != null && place.getUserId() != null){
            request.setAttribute("userId",place.getUserId());
        }

        if(feeTemplate != null){
            WebUtils.setAttribute(request,feeTemplate);
            if(StringTools.isNotBlank(feeTemplate.getPimContent())){
                content = JsonTools.fromJson(feeTemplate.getPimContent(), CDCZPowerIntervalMoneyContent.class);
            }else{
                CDCZCostConfig costConfig = ConfigUtils.getCDCZCostConfig();
                if(StringTools.isNotBlank(costConfig.getPimContent())){
                    content = JsonTools.fromJson(costConfig.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                }
            }
        }else{
            CDCZCostConfig costConfig = ConfigUtils.getCDCZCostConfig();
            feeTemplate = CDCZFeeTemplateService.buildByDefault(costConfig);
            WebUtils.setAttribute(request,feeTemplate);

            if(StringTools.isNotBlank(costConfig.getPimContent())){
                content = JsonTools.fromJson(costConfig.getPimContent(), CDCZPowerIntervalMoneyContent.class);
            }
        }

        if(content != null && content.getItems() != null && content.getItems().size() > 0){
            request.setAttribute("powerIntervalList",content.getItems());
        }

        return "/cdcz/cost/place/config.html";
    }

    @Route(value = { "/cdcz/place/cost/change.do" })
    public AjaxResult doChange(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "保存失败");
        Long placeId = WebUtils.getLong(request,"placeId");
        Long feeTemplateId = WebUtils.getLong(request,"feeTemplateId");

        try {
            if(PlaceBusinessConfigService.self().setFeeTemplateId(placeId,EBusinessType.CDCZ.index(),feeTemplateId)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
